module NianShouGame{
    export class Editor extends egret.DisplayObjectContainer{

        static instance:Editor;

        static Instance():Editor{
            
            if(null == Editor.instance)
            {
                Editor.instance = new Editor();
            }
            return Editor.instance;
        }

        private OnBtnNextLevel(event:egret.TouchEvent):void
        { 
            this.buildingList = [];
            Game.Instance().NextLevel();
        }

        private ExportMapData(event:egret.TouchEvent):void
        {
            let dataStr = "Level_" + Game.Instance().level + " : [";
            let levelData:number[] = [];
            for (let i = 0; i < this.buildingList.length; i++)
            {
                levelData[i] = this.buildingList[i].nameIndex;
                dataStr += this.buildingList[i].nameIndex + ",";
            } 
            dataStr += "],";
            Map.MapData.data[Game.Instance().level] = levelData;
            console.log(dataStr);
        }


        public CreateEditor()
        {
            let btnExportMap = new eui.Button();
            btnExportMap.width = 150;
            btnExportMap.height = 40;
            btnExportMap.label = "导出关卡 " + Game.Instance().level;
            btnExportMap.addEventListener(egret.TouchEvent.TOUCH_TAP, this.ExportMapData, this);
            this.addChild(btnExportMap);

            let btnNextLevel = new eui.Button();
            btnNextLevel.x = 200;
            btnNextLevel.width = 100;
            btnNextLevel.height = 40;
            btnNextLevel.label = "下一关";
            btnNextLevel.addEventListener(egret.TouchEvent.TOUCH_TAP, this.OnBtnNextLevel, this);
            this.addChild(btnNextLevel);

            let btnBeforeLevel = new eui.Button();
            btnBeforeLevel.x = 300;
            btnBeforeLevel.width = 100;
            btnBeforeLevel.height = 40;
            btnBeforeLevel.label = "上一关";
            btnBeforeLevel.addEventListener(egret.TouchEvent.TOUCH_TAP, this.OnBtnBeforeLevel, this);
            this.addChild(btnBeforeLevel);


            // Opera
            let btnCurrentImage = new eui.Button();
            btnCurrentImage.x = 450;
            btnCurrentImage.width = 100;
            btnCurrentImage.height = 40;
            btnCurrentImage.label = Map.MapData.buildName[this.editorBuildIndex];
            this.addChild(btnCurrentImage);

            let btnBeforeImage = new eui.Button();
            btnBeforeImage.x = 400;
            btnBeforeImage.width = 50;
            btnBeforeImage.height = 40;
            btnBeforeImage.label = "<";
            btnBeforeImage.addEventListener(egret.TouchEvent.TOUCH_TAP, 
            function (event:egret.TouchEvent):void{
                btnCurrentImage.label = Map.MapData.buildName[Editor.Instance().BeforeImageIndex()];
            }, 
            btnCurrentImage);
            this.addChild(btnBeforeImage);

            let btnNextImage = new eui.Button();
            btnNextImage.x = 550;
            btnNextImage.width = 50;
            btnNextImage.height = 40;
            btnNextImage.label = ">";
            btnNextImage.addEventListener(egret.TouchEvent.TOUCH_TAP, 
            function (event:egret.TouchEvent):void{
                btnCurrentImage.label = Map.MapData.buildName[Editor.Instance().NextImageIndex()];
            }, 
            btnCurrentImage);
            this.addChild(btnNextImage);
        }

        private OnBtnBeforeLevel(event:egret.TouchEvent):void
        {
            this.buildingList = [];
            Game.Instance().BeforeLevel();
        }


        // 地图编辑器 下一图
        public editorBuildIndex = 0;
        public NextImageIndex():number
        {
            this.editorBuildIndex += 1;
            this.editorBuildIndex = this.editorBuildIndex > Map.MapData.buildName.length - 1 ? 0 : this.editorBuildIndex;
            return this.editorBuildIndex;
        }
        public BeforeImageIndex():number
        {
            this.editorBuildIndex -= 1;
            this.editorBuildIndex = this.editorBuildIndex < 0 ? Map.MapData.buildName.length - 1 : this.editorBuildIndex;
            return this.editorBuildIndex;
        }

        public GetNextBuildImage():any
        {
            let name = Map.MapData.buildName[this.editorBuildIndex] + "_png";
            // console.log("new build:" + name);
            return RES.getRes(name);
        }
        public GetNextGroundImage():any
        {
            let groundName = "G" + Map.MapData.buildName[this.editorBuildIndex].charAt(0) + "_png";
            // console.log("new ground:" + groundName);

            return RES.getRes(groundName);
        }


        public buildingList:Building[] = [];


        public GetNewBuilding(ground:egret.Bitmap, building:egret.Bitmap, buildNameIndex:number):void
        {
            let buildingCount = this.buildingList.length;
            let newBuilding = new Building();
            newBuilding.GetData(ground, building, buildNameIndex);
            newBuilding.ground.touchEnabled = true;
            newBuilding.ground.pixelHitTest = true;
            newBuilding.ground.addEventListener(egret.TouchEvent.TOUCH_END, 
            function(e:egret.TouchEvent):void{
                newBuilding.ground.texture = Editor.Instance().GetNextGroundImage();
                newBuilding.build.texture = Editor.Instance().GetNextBuildImage();
                newBuilding.nameIndex = Editor.Instance().editorBuildIndex;
            }, 
            newBuilding);
            this.buildingList[buildingCount] = newBuilding;
        }
    }
}